package com.tanhua.sso.config;

import com.tanhua.common.exception.BusinessException;
import com.tanhua.common.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: ExceptionAdvice
 * @Author:王晨晗
 * @Description: 统一异常处理类,处理逐层抛出的异常
 * @Date: 2021/12/25/22:21
 */
@ControllerAdvice
public class ExceptionAdvice {

    //处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerException(BusinessException be){
        be.printStackTrace();
        ErrorResult errorResult =  be.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult.getErrMessage());
    }

    //处理未知异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerException1(Exception e){
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
